package demo.boat.boatSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.boat.boatSystem.entity.ShpVesselVoyages;
import demo.boat.boatSystem.service.ShpVesselVoyagesService;
import demo.boat.boatSystem.vo.VesselsVoyagesVO;
import demo.boat.response.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 船舶航次 前端控制器
 * </p>
 *
 * @author boat
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/vesselsVoyages")
public class ShpVesselVoyagesController {

    @Resource
    ShpVesselVoyagesService shpVesselVoyagesService;

    @PostMapping("/findVesselVoyages")
    public Result findVesselVoyages(@RequestParam(required = true, defaultValue = "1") Integer current,
                                     @RequestParam(required = true,defaultValue = "7") Integer size,
                                     @RequestBody VesselsVoyagesVO vesselsVoyagesVO) {
        Page<ShpVesselVoyages> page = new Page<>(current, size);
        QueryWrapper<ShpVesselVoyages> wrapper = getWrapper(vesselsVoyagesVO);
        IPage<ShpVesselVoyages> voyagesPage = shpVesselVoyagesService.findVesselVoyages(page, wrapper);
        long total = voyagesPage.getTotal();
        List<ShpVesselVoyages> records = voyagesPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }
    private QueryWrapper<ShpVesselVoyages> getWrapper(VesselsVoyagesVO vesselsVoyagesVO) {
        QueryWrapper<ShpVesselVoyages> queryWrapper = new QueryWrapper<>();
        String strBegin = vesselsVoyagesVO.getDateBegin();
        String strEnd = vesselsVoyagesVO.getDateEnd();
        /*SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strBegin = ft.format(timeBegin);
        String strEnd = ft.format(timeEnd);*/
        if (vesselsVoyagesVO != null) {
            if (!StringUtils.isEmpty(strBegin)) {

                queryWrapper.ge("VVY_PLAN_END_TM", strBegin);
            }
            if (!StringUtils.isEmpty(strEnd)) {
                queryWrapper.le("VVY_PLAN_END_TM", strEnd);
            }
            queryWrapper.orderByAsc("VVY_PLAN_END_TM");
            /*if (timeBegin != null && timeEnd != null) {
                queryWrapper.between("VVY_PLAN_END_TM)", strBegin, strEnd);
            }*/
        }
        return queryWrapper;
    }


}

